import { defineStore } from "pinia";
import { ref } from "vue";
import { loginApi } from "@/apis/user";
import { useCartStore } from '@/stores/cartStore'
export const useUserStore = defineStore(
  "user",
  () => {
    const cartStore = useCartStore()
    // 用户信息
    const userInfo = ref({});

    // 获取用户信息
    const getUserInfo = async (form) => {
      // 调用登录接口
      const res = await loginApi(form);
      userInfo.value = res.result;
      //合并购物车列表(本地购物车+云端购物车)
      cartStore.mergeCart()
    };
    // 退出登录 清空信息
    const clearUserInfo = () => {
      userInfo.value = {};
      cartStore.clearCart()
    };
    return { userInfo, getUserInfo, clearUserInfo };
  },
  {
    persist: true,
  }
);
